<?php
require_once("classes/db.class.php");
require_once 'classes/ask.class.php';
require_once 'classes/work.class.php';
include 'mysql.config.php';
session_start();
$db = new DataBase($db_address, $db_username, $db_password, $db_db);
if(isSet($_POST['last_ask']))
{
    $pg = ($_POST['last_ask'] + 1);
$last_ask=$pg*4;
$order = $_POST['order'];
$filter = $_POST['filter'];
$filter = stripslashes($filter);
$last_ask=mysql_real_escape_string($last_ask);
$sql = "SELECT DISTINCT(`a`.`workid`),`a`.`id`, COUNT(*) AS `sF`, SUM(`a`.`finished`) AS `sumfinished`,`w`.`uid`,`w`.`catid` FROM `asks` AS `A`, `works` AS `W` WHERE $filter `w`.`uid`='" . $_SESSION['uid'] . "' AND `w`.`id`=`a`.`workid`  GROUP BY `w`.`id` $order LIMIT $last_ask,4";
$result=$db->query($sql) or die(mysql_error());

while($asks=$db->fetch($result))
{
    $aid = $asks->id;
    
    $ask = new Ask($aid);
    $wid = $ask->getWorkId();
    $sentFeedbacks = $asks->sF;
    $gotFeedbacks = $asks->sumfinished;
    $work = new Work($wid);
    require_once 'classes/category.class.php';
    $cat = new Category($work->getCategoryId());
        $datep = date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d", strtotime($work->getDate())))) . " +2 week"));
        $datec = date("Y-m-d");
        if ($gotFeedbacks == $sentFeedbacks)
            $str = "<span class=\"label success\">Finished</span>";
        elseif ($datep > $datec) {
            if ($gotFeedbacks < $sentFeedbacks)
                $str = "<span class=\"label warning\"> Awaiting for feedbacks</span>";
        }
        else
            $str = "<span class=\"label import\"> No time left</span>";
        echo "<li style='border-style:solid;border-width:1px;border-color:#DDD;padding:10px;margin:5px;background-color:#f7f7f7;display:inline-table;width: 250px;max-width: 250px;height:210px;'>";

        echo "<p style='margin:0;font-size:11px;'>" . $work->getDate() . " $str</p>";
        echo "<p style='margin:0;font-size:11px;'><strong>" . $cat->getName() . "</strong></p>";
        echo "<div style='min-height:30px;max-height:70px;height:45px;border:0;padding:0;margin:0;'><p style='line-height:15px;font-size:11px;display:block;'><i>" . substr($work->getDescription(), -100) . "...</i></p></div>";
        echo "<p style='margin:0;font-size:11px;'>Got <strong>$gotFeedbacks</strong> of <strong>$sentFeedbacks</strong> feedbacks </p>";
        $sql2 = "SELECT DISTINCT `feedbacks`.`id`, `feedbacks`.`workid`,`comment`,`feedbacks`.`uid`, AVG(`speed`) AS `speed`, AVG(`quality`) AS `quality` FROM `feedbacks` INNER JOIN `works` ON `works`.`id`=`feedbacks`.`workid` WHERE `works`.`id` = '$wid'";
        $query2 = $db->query($sql2);
        $res = $db->fetch($query2);
        $s = round($res->speed);
        $q = round($res->quality);
        ?>
        <table cellpadding="0" cellspacing="0" style="width:200px;">
            <tr style="height:30px">
                <td style="padding:5px 3px 3px 6px;margin:0;width: 50px">Speed</td>
                <td style="padding:5px 3px 3px 6px;;margin:0">
                    <?php
                    for ($c = 0; $c < $s; $c++)
                        echo "<img style='width:16px;' src='star_48.png'/>";
                    $c = 0;
                    $s = 0;
                    ?>
                </td>
            </tr>
            <tr style="height:30px">
                <td style="padding:5px 3px 3px 6px;margin:0">
                    Quality 
                </td>
                <td style="padding:5px 3px 3px 6px;;margin:0">
                    <?php
                    for ($c = 0; $c < $q; $c++)
                        echo "<img style='width:16px;' src='star_48.png'/>";
                    $c = 0;
                    $q = 0;
                    ?>
                </td>
            </tr>
        </table>
        <?php
        
       // $last_ask = $aid;
        echo "<p><a href='index.php?p=feedback&wid=$wid' style='font-size:11px;'>See detailed feedback</a></p>";
        echo "</li>";
}

//echo $last_ask;
?>

<div id="more<?php echo $pg; ?>" class="morebox" style="padding-left:5px;">
<a href="#" id="<?php echo $pg; ?>" class="btn more">Show more</a>
<input type="hidden" name="order" id="orders<?php echo $pg; ?>" value="<?php echo $order;?>"/>
<input type="hidden" name="filter" id="filters" value="<?php echo $filter;?>"/>
</div>
<?php
}

?>